import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import Login from "../views/LoginView.vue";
import store from "../store/index";

Vue.use(VueRouter, store);

const routes = [
  {
    path: "/",
    name: "home",
    redirect: "/comprehensive",
    component: HomeView,
    // children: [
    //   //综合态势
    //   {
    //     path: "comprehensive",
    //     name: "comprehensive",
    //     component: () =>
    //       import("../views/comprehensive/ComprehensiveSituational.vue"),
    //   },
    //   //设备管理
    //   {
    //     path: "equipment",
    //     name: "equipment",
    //     component: () => import("../views/equipment/EquipmentManagement.vue"),
    //     redirect: "/equipment/fireDetection",
    //     children: [
    //       // 火灾监测报警系统
    //       {
    //         path: "fireDetection",
    //         name: "fireDetection",
    //         component: () => import("../views/equipment/fireDetection.vue"),
    //       },
    //       // 消防水监测系统
    //       {
    //         path: "fireWater",
    //         name: "fireWater",
    //         component: () => import("../views/equipment/fireWater.vue"),
    //       },
    //       // 室外消防栓系统
    //       {
    //         path: "outdoorFirefighting",
    //         name: "outdoorFirefighting",
    //         component: () =>
    //           import("../views/equipment/outdoorFirefighting.vue"),
    //       },
    //       // 智慧安全用电系统
    //       {
    //         path: "safeElectricity",
    //         name: "safeElectricity",
    //         component: () => import("../views/equipment/safeElectricity.vue"),
    //       },
    //       // 视频监控系统
    //       {
    //         path: "videoSurveillance",
    //         name: "videoSurveillance",
    //         component: () => import("../views/equipment/videoSurveillance.vue"),
    //       },
    //     ],
    //   },
    //   //告警管理目录
    //   {
    //     path: "alarm",
    //     name: "alarm",
    //     redirect: "/alarm/AlarmAll",
    //     component: () => import("../views/alarm/AlarmIndex.vue"),
    //     children: [
    //       //告警管理-所有
    //       {
    //         path: "AlarmAll",
    //         name: "AlarmAll",
    //         component: () => import("../views/alarm/AlarmAll.vue"),
    //       },
    //       //告警管理-综合办公楼
    //       {
    //         path: "AlarmManagement",
    //         name: "AlarmManagement",
    //         component: () => import("../views/alarm/AlarmManagement.vue"),
    //       },
    //       // 告警详情
    //       {
    //         path: "AlarmDetails",
    //         name: "AlarmDetails",
    //         component: () => import("../views/alarm/AlarmDetails"),
    //       },
    //       // 产线产房
    //       {
    //         path: "ProductionHouse",
    //         name: "ProductionHouse",
    //         component: () => import("../views/alarm/ProductionHouse"),
    //       },
    //       // 仓储楼
    //       {
    //         path: "WarehouseFloor",
    //         name: "WarehouseFloor",
    //         component: () => import("../views/alarm/WarehouseFloor"),
    //       },
    //       // 活动中心
    //       {
    //         path: "ActivityCenter",
    //         name: "ActivityCenter",
    //         component: () => import("../views/alarm/ActivityCenter"),
    //       },
    //     ],
    //   },
    //   //故障管理
    //   {
    //     path: "fault",
    //     name: "fault",
    //     redirect: "/fault/allequipment",
    //     component: () => import("../views/fault/FaultManagement.vue"),
    //     children: [
    //       {
    //         path: "/fault/allequipment",
    //         name: "page0",
    //         component: () => import("../views/fault/AllEquipment.vue"),
    //       },
    //       {
    //         path: "/fault/firequipment",
    //         name: "page1",
    //         component: () => import("../views/fault/FireEuipment.vue"),
    //       },
    //       {
    //         path: "/fault/water",
    //         name: "page2",
    //         component: () => import("../views/fault/WaterEuipment.vue"),
    //       },
    //       {
    //         path: "/fault/door",
    //         name: "page3",
    //         component: () => import("../views/fault/DoorEuipment.vue"),
    //       },
    //       {
    //         path: "/fault/safe",
    //         name: "page4",
    //         component: () => import("../views/fault/SafeEuipment.vue"),
    //       },
    //       {
    //         path: "/fault/video",

    //         component: () => import("../views/fault/VideoEuipment.vue"),
    //       },
    //     ],
    //   },
    //   //值班室管理
    //   {
    //     path: "duty",
    //     name: "duty",
    //     component: () => import("../views/duty/DutyManagement.vue"),
    //     redirect: "/duty/DutyRoomMonitoring",
    //     children: [
    //       {
    //         path: "DutyRoomMonitoring",
    //         name: "DutyRoomMonitoring",
    //         component: () => import("../views/duty/DutyRoomMonitoring.vue"),
    //       },
    //       {
    //         path: "ShiftManagement",
    //         name: "ShiftManagement",
    //         component: () => import("../views/duty/ShiftManagement.vue"),
    //       },
    //       {
    //         path: "DutyRecords",
    //         name: "DutyRecords",
    //         component: () => import("../views/duty/DutyRecords.vue"),
    //       },
    //     ],
    //   },
    //   // 隐患管理
    //   {
    //     path: "Risk",
    //     name: "Risk",
    //     redirect: "/risk/RiskList",
    //     component: () => import("../views/risk/RiskIndex.vue"),
    //     children: [
    //       // 顶部card区
    //       {
    //         path: "RiskManagement",
    //         name: "RiskManagement",
    //         component: () => import("../views/risk/RiskManagement.vue"),
    //       },
    //       // 内容表格区
    //       {
    //         path: "RiskList",
    //         name: "RiskList",
    //         component: () => import("../views/risk/RiskList.vue"),
    //       },
    //       // 详情区
    //       {
    //         path: "RiskDetail",
    //         name: "RiskDetail",
    //         component: () => import("../views/risk/RiskDetail"),
    //       },
    //       // 综合办公楼
    //       {
    //         path: "ComplexOfficeBuilding",
    //         name: "ComplexOfficeBuilding",
    //         component: () => import("../views/risk/ComplexOfficeBuilding"),
    //       },
    //       // 产线产房
    //       {
    //         path: "ProductionHouse",
    //         name: "ProductionHouse",
    //         component: () => import("../views/risk/ProductionHouse"),
    //       },
    //       // 仓储楼
    //       {
    //         path: "WarehouseFloor",
    //         name: "WarehouseFloor",
    //         component: () => import("../views/risk/WarehouseFloor"),
    //       },
    //       // 活动中心
    //       {
    //         path: "ActivityCenter",
    //         name: "ActivityCenter",
    //         component: () => import("../views/risk/ActivityCenter"),
    //       },
    //     ],
    //   },
    //   //巡检管理
    //   {
    //     path: "inspection",
    //     name: "inspection",
    //     component: () => import("../views/inspection/InspectionManagement.vue"),
    //     redirect: "/inspection/patrolScheme",
    //     children: [
    //       {
    //         path: "patrolScheme",
    //         name: "patrolScheme",
    //         component: () => import("../views/inspection/patrolScheme.vue"),
    //       },
    //       // 巡检任务
    //       {
    //         path: "InspectionTasks",
    //         name: "InspectionTasks",
    //         component: () => import("../views/inspection/InspectionTasks.vue"),
    //       },
    //       // 巡检点管理
    //       {
    //         path: "patrolPoints",
    //         name: "patrolPoints",
    //         component: () => import("../views/inspection/patrolPoints.vue"),
    //         redirect: "/inspection/patrolPoints/all",
    //         children: [
    //           // 全部
    //           {
    //             path: "all",
    //             name: "all",
    //             component: () => import("../components/inspection/all.vue"),
    //           },
    //           // 综合办公楼
    //           {
    //             path: "integratedOffice",
    //             name: "integratedOffice",
    //             component: () =>
    //               import("../components/inspection/integratedOffice.vue"),
    //           },
    //           // 产线厂房
    //           {
    //             path: "productionLine",
    //             name: "productionLine",
    //             component: () =>
    //               import("../components/inspection/productionLine.vue"),
    //           },
    //           // 仓储楼
    //           {
    //             path: "storage",
    //             name: "storage",
    //             component: () => import("../components/inspection/storage.vue"),
    //           },
    //           // 活动中心
    //           {
    //             path: "activityCenter",
    //             name: "activityCenter",
    //             component: () =>
    //               import("../components/inspection/activityCenter.vue"),
    //           },
    //         ],
    //       },
    //       // 人员定位
    //       {
    //         path: "personnelLocation",
    //         name: "personnelLocation",
    //         component: () =>
    //           import("../views/inspection/personnelLocation.vue"),
    //       },
    //     ],
    //   },
    // ],
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  //错误页面
  {
    path: "*",
    component: () => import("../views/system/Error.vue"),
  },
];
const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
router.beforeEach((to, from, next) => {
  //一开始跳转到登录页面
  let token = sessionStorage.getItem("token");
  if (!token) {
    if (to.path == "/comprehensive") {
      next("login");
    }
  }
  let power = sessionStorage.getItem("power");

  //按钮权限
  switch (power) {
    case "超级管理员":
      store.state.buttonPower.add = true;
      store.state.buttonPower.del = true;
      store.state.buttonPower.edit = true;
      store.state.power = true;
      break;
    case "普通角色":
      store.state.buttonPower.add = true;
      store.state.buttonPower.del = false;
      store.state.buttonPower.edit = false;
      store.state.power = false;
      break;
    case "访客":
      store.state.buttonPower.add = false;
      store.state.buttonPower.del = false;
      store.state.buttonPower.edit = false;
      store.state.power = false;
      break;
  }
  if (to.name == "login") {
    sessionStorage.clear();
    next();
  } else {
    if (power) {
      let list = JSON.parse(sessionStorage.getItem("list"));
      if (list) {
        let routerArr = router.getRoutes();
        if (routerArr.length > 3) {
          next();
        } else {
          list.forEach((v) => {
            if (v.href == "/comprehensive") {
              router.addRoute("home", {
                path: v.href,
                name: v.name,
                component: () => import(`@/views/${v.view}.vue`),
              });
            } else {
              router.addRoute("home", {
                path: v.href,
                name: v.name,
                component: () => import(`@/views/${v.view}.vue`),
                redirect: v.children[0].href,
              });
              v.children.forEach((item) => {
                router.addRoute(v.name, {
                  path: item.href,
                  name: item.name,
                  component: () => import(`@/views/${item.view}.vue`),
                });
                if (item.children) {
                  item.children.forEach((child) => {
                    router.addRoute(item.name, {
                      path: child.href,
                      component: () => import(`@/${child.view}.vue`),
                    });
                  });
                  if (to.path == "/inspection/patrolPoints") {
                    console.log("成功到达");
                    next("/inspection/patrolPoints/all");
                  }
                }
              });
            }
          });

          console.log(router.getRoutes(), "看这里111");
          next({ ...to, replace: true });
        }
      }
    }
  }
});
export default router;
